import numpy as np
import random

dataset = np.load(r'C:\Users\31956\Documents\coding\tf-gan\src\data\data(11149,20,3).npy',
                  allow_pickle=True).tolist()

tup_dict = {}
for line in dataset:
    for t in line:
        strkey = "{}|{}|{}".format(t[0], t[1], t[2])
        if strkey not in tup_dict.keys():
            tup_dict[strkey] = 1
        else:
            tup_dict[strkey] += 1
total_times = 0
for key in tup_dict:
    total_times += tup_dict[key]

for key in tup_dict:
    tup_dict[key] = tup_dict[key] / total_times
N = 800 * 20 *2
# print(tup_dict)

p_array = list(tup_dict.keys())
plist = list(tup_dict.values())

n = len(p_array)

Psample = []
mySample = []

index = int(random.random() * n)
mw = max(plist)
beta = 0.0

for i in range(N):  ##核心算法
    beta = beta + random.random() * 2.0 * mw
    while beta > plist[index]:
        beta = beta - plist[index]
        index = (index + 1) % n
    Psample.append(plist[index])
    tup = []
    for i in p_array[index].split('|'):
        tup.append(float(i))
    # 将切分后的每个元素转成float再储存 '76.0|0.25|0.0'
    mySample.append(tup)

# cresult = Counter(Psample)
# print('Psample : {}'.format(Psample))
# psam = [cresult[x] for x in plist]
# pe = [x * N for x in plist]

out_data = np.array(mySample).reshape((800, 20, -1))
np.save(r'data/random800_20_6v5',out_data)
